let routes= [
		{
			path: '/',
			component:'layout',
			name:'layout',
			redirect: {
			  name: 'index_list'
			},
			children:[
				{
					meta:{
						title:'首页'
					},
					component:'index/list',
				},
				{
					meta:{
						title:'最近使用'
					},
					component:'lastuse/index',
				}
				
			]
		},
		{
			path:'*',
			redirect:{
				name:'index_list'
			}
		}
]

let getRoutes = function() {
  createRoute(routes)
  return routes
}
	
function createRoute(arr){
	arr.forEach((item,index)=>{
		
		if(!item.component) return
		let val = getValue(item.component)
		
		item.name = item.name || val.replace(/\//g,'_')
		item.path = item.path || `/${val}`
		
		let componentFun = import(`@/views/${item.component}.vue`)
		item.component = ()=>componentFun
		
		if(item.children && item.children.length>0){
			createRoute(item.children)
		}
	})
}

//有index则去掉index
function getValue(comp){

	let index = comp.lastIndexOf('/')
	let val = comp.substring(index+1)
	
	if(val==='index'){
		return comp.substring(index,0)
	}

	return comp
}




export default getRoutes()


